const cloud = getApp().cloud;
const app = getApp();
Page({
  data: {
    imgArr:[
      'https://img.alicdn.com/tps/TB1sXGYIFXXXXc5XpXXXXXXXXXX.jpg',
      'https://img.alicdn.com/imgextra/i4/4161056916/O1CN01fwxF2720xZqjfRo88_!!4161056916.jpg',
      'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01YXNLRx20xZqjfm50f_!!4161056916.png',
      'https://img.alicdn.com/tps/TB1sXGYIFXXXXc5XpXXXXXXXXXX.jpg',
      'https://img.alicdn.com/imgextra/i4/4161056916/O1CN01fwxF2720xZqjfRo88_!!4161056916.jpg',
      'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01YXNLRx20xZqjfm50f_!!4161056916.png',
      'https://img.alicdn.com/tps/TB1sXGYIFXXXXc5XpXXXXXXXXXX.jpg',
      'https://img.alicdn.com/imgextra/i4/4161056916/O1CN01fwxF2720xZqjfRo88_!!4161056916.jpg',
      'https://img.alicdn.com/imgextra/i2/4161056916/O1CN01YXNLRx20xZqjfm50f_!!4161056916.png',
      'https://img.alicdn.com/tps/TB1sXGYIFXXXXc5XpXXXXXXXXXX.jpg',
    ],   //下载图片数组
    img:'https://img.alicdn.com/imgextra/i1/4161056916/O1CN01M3L13z20xZrbRUQkQ_!!4161056916.png',   //显示图片
    upWorks:false,        //弹窗
    strategy:false,        //攻略弹窗
    uploadImgPop:false,     //上传作品弹窗
    width:614
  },

  // 图片下载
  downImg(e){

    // for(let i in this.data.imgArr){
    //   console.log(i)
    //   my.saveImage({
    //     url: this.data.imgArr[i],
    //     showActionSheet: true,
    //     success: () => {
    //       // my.alert({
    //       //   title: '保存成功',
    //       // });
    //     },
    //   });
    // }
    // my.alert({content:"保存成功"})

    my.saveImage({
      url: this.data.imgArr[e.currentTarget.dataset.type],
      showActionSheet: true,
      success: () => {
        my.alert({
          title: '下载成功',
        });
      },
    });
  },

  // 上传
  upImg(){
    my.chooseImage({
      count: 1,
      success: (res) => {
        console.log("图片链接",res)

        my.getImageInfo({
          src:res.apFilePaths[0],
          success:(res)=>{
            console.log("图片信息",res);
            if(res.width == 720 && res.height == 1120){

              console.log(100);
              
              my.compressImage({
                apFilePaths:[res.path],
                compressLevel:1,
                success:(r)=>{
                  console.log("压缩",JSON.stringify(r));

                  this.setData({
                    uploadImgPop:true
                  })

                  cloud.file.uploadFile({ filePath: r.apFilePaths[0], fileType: 'image' }).then(ress => { 

                    console.info('上传',JSON.stringify(ress))

                    setTimeout(()=>{
                      this.setData({
                        img:ress.url,
                        uploadImgPop:false,
                        scbs:1,
                        width:230
                      })
                    },2000)

                  })

      
                  // my.getImageInfo({
                  //   src:r.apFilePaths[0],
                  //   success:(ress)=>{

                  //     console.log('上传',JSON.stringify(ress))

                  //     this.setData({
                  //       img:ress.path,
                  //       scbs:1,
                  //       width:230
                  //     })

                  //   }
                  // })

                }
              })


            }else{
              my.showToast({
                content:"图片尺寸不符，请上传1600*900的图片！"
              });
            }
          }
        })
      },
    });
  },


  change(e){
    
    console.log(e);

    this.setData({
      [e.currentTarget.dataset.name]:e.detail.value
    })

    console.log(this.data)

  },

  //提交信息
  tjxx(){

    if(this.data.name){

      if(this.data.phone){

        if(this.data.phone.length >= 16){

          my.alert({
            content:"电话号码格式有误"
          });

          return

        }
        
        // 正则
        // 保存接口
        // 关闭弹窗
        // 打开上传中的弹窗
        // 成功跳转下一页

        this.setData({
          upWorks:false
        })

      }else{
        my.alert({
          content:"您还未填写电话号码"
        });
      }
 
    }else{
      my.alert({
        content:"您还未填写名称"
      });
    }
  },
  strategy(){
    this.setData({
      strategy:true
    })
  },

  // 关闭
  onClose(e){
    if(e.currentTarget.dataset.type == "upWorks"){
      this.setData({
        upWorks:false
      })
    }else{
      this.setData({
        strategy:false
      })
    }
    
  },


  //上传作品
  upWorks(){
    //需判断是否有上传图片

    if(this.data.scbs == 1){

      console.log(this.data.img);

      cloud.function.invoke('client',{url:this.data.img},'uploadWork').then(resulte => {
        console.log('uploadImg---上传作品',resulte);  

        if(resulte.code == 0){

          my.showToast({content:'上传作品成功！'});

          my.redirectTo({
            url: `../resulte/resulte?img=${this.data.img}`
          })

        }
      
      }) 


    }else{
      my.alert({
        content:"您还未填写上传作品"
      });
    }
   
   
  },
  

  //回到首页
  goIndex(){
    // 关闭所有页面回到首页
    my.reLaunch({
      url: '../index/index'
    })

  },

  onLoad() {},
});
